const Koa = require('koa')
const path = require('path')
const { sep } = path
const middlewareLoader = require('./loader/middleware')
const routerSchemaLoader = require('./loader/routerSchema')
const controllerLoader = require('./loader/controller')
const serviceLoader = require('./loader/service')
const configLoader = require('./loader/config')
const extendLoader = require('./loader/extend')
const routerLoader = require('./loader/router')
module.exports = {
  start(options = {}) {
    try {
      const app = new Koa()
      app.options = options
      app.baseDir = process.cwd()
      app.businessPath = path.resolve(app.baseDir, `.${sep}app`)
      // app.env = env()
      middlewareLoader(app)
      routerSchemaLoader(app)
      controllerLoader(app)
      serviceLoader(app)
      configLoader(app)
      extendLoader(app)
      routerLoader(app)
      const port = process.env.PORT || 8000
      const host = process.env.HOST || '0.0.0.0'
      app.listen(port, host, () => {
        console.log(`Server listening on http://${host}:${port}`)
      })
    } catch (error) {
      console.error(error)
    }
  },
}
